/*
* Hua-Xia System Messager(Base on Prototype 1.5)
* Author TrueOrFalse.Yuan
* Version 2.0
* Date 2007-07-10
*/
var message = Class.create();
message.prototype = {
    initialize : function(id,message,type) {
        this._id = id;
        this._message = message;
        this._type = type;
    },
    getHTML : function(){
        return '<div id="'+ this._id +'" class="' + this._type.toLowerCase() + '">' + this._message + '</div>';
    }
}

var messager = Class.create();
messager.prototype = {
    initialize : function(parent,options) {
        this._parent = $(parent);
        this._options = Object.extend({
    		messageType : 'INFORMATION',
    		isImmediate : false,
    		onChange : function(){}		
    	}, options || {});
    },
    messageGroup : $H(),
    addMessage : function(id,msg){
        var msg_add = {};
		msg_add[id] = new message(id,msg,this._options.messageType);
		Object.extend(this.messageGroup, msg_add);
		if(this._options.isImmediate){
            this._parent.show();
            if($(id)){
                $(id).remove();
            }
            new Insertion.Bottom(this._parent.id, msg_add[id].getHTML());
            this._options.onChange();
		}		
    },
    removeMessage : function(id){
        this.messageGroup.remove(id);
        if(this._options.isImmediate){
            if($(id)){
                $(id).remove();
                if(!this.messageGroup.size()){
                    this._parent.hide();
                }
                this._options.onChange();
            }
		}
    },
    showMessages : function(){
        if(!this._options.isImmediate){
            var mg = this.messageGroup;
            var p = this._parent;
            mg.keys().each(function(id) {
                new Insertion.Bottom(id, mg[id].getHTML());
            });
            if(mg.size()){
                p.show();
            }
            this._options.onChange();
        }
    },
    clearMessages : function(){
        var mg = this.messageGroup;
        mg.keys().each(function(id) {
            mg.remove(id);
            if($(id)){
                $(id).remove();
            }
        });
        if(!mg.size()){
            this._parent.hide();
        }
        this._options.onChange();
    }
}
    